package backend.db;

import java.sql.Types;

public class Column {
  final String name;
  private final String type;
  final int intType;
  
  public Column(final String _name, final String _type) {
    this(_name, _type, false);
  }
  
  public Column(final String _name, final String _type, final boolean isPrimaryKey) {
    this(_name, _type, isPrimaryKey, false);
  }
  
  public Column(final String _name, final String _type, final boolean isPrimaryKey, final boolean isUniqueKey) {
    this(_name, _type, isPrimaryKey, isUniqueKey, true);
  }
  
  public Column(final String _name, final String _type, final boolean isPrimaryKey,
      final boolean isUniqueKey, final boolean notNull) {
    name = _name;
    final StringBuilder typeSb = new StringBuilder(_type);
    if (notNull)
      typeSb.append(" NOT NULL");
    if (isPrimaryKey)
      typeSb.append(" PRIMARY KEY");
    else if (isUniqueKey)
      typeSb.append(" UNIQUE KEY");
    type = typeSb.toString();
    intType = getIntType(_type);
  }
  
  private int getIntType(final String _type) {
    if (_type.startsWith("VARCHAR(") || "TEXT".equals(_type))
      return Types.VARCHAR;
    else if ("TINYINT".equals(_type))
      return Types.TINYINT;
    else if ("SMALLINT".equals(_type))
      return Types.SMALLINT;
    else if ("TIME".equals(_type))
      return Types.TIME;
    else if ("DATE".equals(_type))
      return Types.DATE;
    else if ("BOOLEAN".equals(_type))
      return Types.BOOLEAN;
    throw new RuntimeException();
  }
  
  @Override public String toString() {
    return name + " " + type;
  }
}
